﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using QLKhachSan.DTO;
using System.Data.SqlClient;
using QLKhachSan.DAO;
using QLKhachSan.BUS;
using DevExpress.XtraEditors;

namespace QLKhachSan.GUI
{
    public partial class fBangKeDichVu : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public fBangKeDichVu()
        {
            InitializeComponent();
        }
        private Boolean checkThemDV;
        private int _MaDV;      
        private int maBangKe;
        private int maCTBangKe;        
        private void textBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar) && (e.KeyChar != '.'))
                e.Handled = true;
        }

        private void fBangKeDichVu_Load(object sender, EventArgs e)
        {
           
            //Chi Tiet Bang Ke
            textBoxSL.Enabled = false;
            textBoxSL.ResetText();
            textBoxTenP.Enabled = false;
            textBoxTenP.Text = fDanhSachPhong._TenP;
            textBoxTenDV.Enabled = false;
            textBoxTenDV.ResetText();
            dateTimePickerThoiGian.Enabled = false;

            simpleButtonHuyP.Enabled = false;
            simpleButtonLuuP.Enabled = false;
            simpleButtonSuaP.Enabled = true;
            simpleButtonThemP.Enabled = true;
            simpleButtonXoaP.Enabled = true;
            checkThemDV = false;
            // Ten Khach Hang
            textBoxTenKH.Enabled = false;
          
            DataTable dta_kh = BUS_KhachHang.hienthiDanhSachKhachHang();
            for (int j = 0; j < dta_kh.Rows.Count; j++)
            {
                if (int.Parse(dta_kh.Rows[j]["MaKH"].ToString()) == fDanhSachPhong.maKH)
                {
                    textBoxTenKH.Text = dta_kh.Rows[j]["TenKH"].ToString();
                    break;
                }
            }
            //Bang Ke DV
            maBangKe = -1;
            DataTable dta_bk = BUS_BangKeDichVu.hienthiDanhSachBangKeDichVu();
            for (int i = 0; i < dta_bk.Rows.Count; i++)
            {
                if (int.Parse(dta_bk.Rows[i]["MaKH"].ToString()) == fDanhSachPhong.maKH)
                {
                    maBangKe = int.Parse(dta_bk.Rows[i]["MaBangKe"].ToString());
                    break;
                }
            }
            if(maBangKe != -1 )
            {                
                DTO_CTBangKeDichVu dto_ctbk = new DTO_CTBangKeDichVu();
                dto_ctbk.MaBangKe = maBangKe;
                dataGridViewBK.DataSource = BUS_CTBangKeDichVu.hienthiDanhSachCTBangKeDichVuTheoMaBangKe(dto_ctbk);
            }
            for (int i = 0; i < dataGridViewBK.Rows.Count - 1; i++)
            {
                dataGridViewBK.Rows[i].Cells[0].Value = i + 1;
            }
            comboBoxTenLoaiDV.Items.Clear();
            comboBoxTenLoaiDV.Enabled = false;
            DataTable dt_LoaiDichVu = BUS_LoaiDichVu.hienthiDanhSachLoaiDichVu();
            for (int i = 0; i < (dt_LoaiDichVu.Rows.Count); i++)
            {
                comboBoxTenLoaiDV.Items.Add(dt_LoaiDichVu.Rows[i]["TenLoaiDV"]);
            }
            comboBoxTenLoaiDV.Text = dt_LoaiDichVu.Rows[0]["TenLoaiDV"].ToString();
            //DS Dich Vu
            dataGridViewDS.DataSource =  BUS_DichVu.hienthiDanhSachDichVu(); 


        }

        private void dataGridViewDS_MouseClick(object sender, MouseEventArgs e)
        {
          try
            {               
                    int r = dataGridViewDS.CurrentCell.RowIndex;
                    textBoxTenDV.Text = dataGridViewDS.Rows[r].Cells["TenDV"].Value.ToString();
                    _MaDV = int.Parse(dataGridViewDS.Rows[r].Cells["MaDV"].Value.ToString());
                    textBoxSL.ResetText();
            }
            catch (Exception)
            {
            }
        }

        private void simpleButtonThemP_Click(object sender, EventArgs e)
        {
            comboBoxTenLoaiDV.Enabled = true;
            simpleButtonHuyP.Enabled = true;
            simpleButtonLuuP.Enabled = true;
            simpleButtonXoaP.Enabled = false;
            simpleButtonSuaP.Enabled = false;
            simpleButtonThemP.Enabled = false;
            textBoxSL.ResetText();
            textBoxSL.Enabled = true;            
            checkThemDV = true;
        }

        private void simpleButtonSuaP_Click(object sender, EventArgs e)
        {
           if(textBoxTenDV.Text != "" )
           {
                simpleButtonHuyP.Enabled = true;
                simpleButtonLuuP.Enabled = true;
                simpleButtonXoaP.Enabled = false;
                simpleButtonSuaP.Enabled = false;
                simpleButtonThemP.Enabled = false;
                textBoxSL.Enabled = true;            
                checkThemDV = false;
                comboBoxTenLoaiDV.Enabled = true;
           }
            else
           {
               MessageBox.Show(" Chưa chọn Dịch vụ ở Bảng Chi tiết Bảng kê dịch vụ ", " Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Information);
           }
        }

        private void simpleButtonHuyP_Click(object sender, EventArgs e)
        {
            comboBoxTenLoaiDV.Enabled = false;
            textBoxSL.Enabled = false;
            textBoxSL.ResetText();
            textBoxTenP.Enabled = false;          
            textBoxTenDV.Enabled = false;
            textBoxTenDV.ResetText();
            dateTimePickerThoiGian.Enabled = false;
            simpleButtonHuyP.Enabled = false;
            simpleButtonLuuP.Enabled = false;
            simpleButtonSuaP.Enabled = true;
            simpleButtonThemP.Enabled = true;
            simpleButtonXoaP.Enabled = true;
            checkThemDV = false;
            DTO_CTBangKeDichVu dto_ctbk = new DTO_CTBangKeDichVu();
            dto_ctbk.MaBangKe = maBangKe;
            dataGridViewBK.DataSource = BUS_CTBangKeDichVu.hienthiDanhSachCTBangKeDichVuTheoMaBangKe(dto_ctbk);
            for (int i = 0; i < dataGridViewBK.Rows.Count - 1; i++)
            {
                dataGridViewBK.Rows[i].Cells[0].Value = i + 1;
            }

        }

        private void simpleButtonLuuP_Click(object sender, EventArgs e)
        {
            if(textBoxSL.Text.Trim() != "")
            {
                DTO_CTBangKeDichVu dto_ctbk = new DTO_CTBangKeDichVu();
                if (maBangKe < 0)
                {
                    DTO_BangKeDichVu dto_bk = new DTO_BangKeDichVu();
                    dto_bk.MaKH = fDanhSachPhong.maKH;
                    BUS_BangKeDichVu.themBangKeDichVu(dto_bk);
                    DataTable dta_bk = BUS_BangKeDichVu.hienthiDanhSachBangKeDichVu();
                    dto_ctbk.MaBangKe = int.Parse(dta_bk.Rows[dta_bk.Rows.Count - 1]["MaBangKe"].ToString());
                    maBangKe = int.Parse(dta_bk.Rows[dta_bk.Rows.Count - 1]["MaBangKe"].ToString());
                }
                else
                {
                    dto_ctbk.MaBangKe = maBangKe;                  
                }
                dto_ctbk.MaDV = _MaDV;
                dto_ctbk.SoLuong = int.Parse(textBoxSL.Text);
                dto_ctbk.ThoiGian = dateTimePickerThoiGian.Value;
                if (checkThemDV)
                {
                    BUS_CTBangKeDichVu.themCTBangKeDichVu(dto_ctbk);
                    MessageBox.Show(" Thêm dịch vụ thành công ", " Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    simpleButtonHuyP_Click(sender, e);
                } else 
                {
                    dto_ctbk.MaCTBangKe = maCTBangKe;
                    BUS_CTBangKeDichVu.suaCTBangKeDichVu(dto_ctbk);
                    MessageBox.Show(" Sửa dịch vụ thành công ", " Thông báo!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    simpleButtonHuyP_Click(sender, e);
                }
                

            }
            else
            {
            MessageBox.Show("Chưa điền thông tin về Số lượng?", "Lỗi !", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void dataGridViewBK_MouseClick(object sender, MouseEventArgs e)
        {
            try
            {
                if (!checkThemDV)
                {
                    int r = dataGridViewBK.CurrentCell.RowIndex;
                    textBoxTenDV.Text = dataGridViewBK.Rows[r].Cells["TenDichVu"].Value.ToString();
                    _MaDV = int.Parse(dataGridViewBK.Rows[r].Cells["MaDichVu"].Value.ToString());
                    maCTBangKe = int.Parse(dataGridViewBK.Rows[r].Cells["MaChiTietBangKe"].Value.ToString());
                    dateTimePickerThoiGian.Text = dataGridViewBK.Rows[r].Cells["ThoiGian"].Value.ToString();
                    textBoxSL.Text = dataGridViewBK.Rows[r].Cells["SoLuong"].Value.ToString();
                   
                }
            }
            catch (Exception)
            {
            }
        }

        private void simpleButtonXoaP_Click(object sender, EventArgs e)
        {
            if (textBoxTenDV.Text.Trim() != "" && maCTBangKe > 0 && textBoxSL.Text !="")
            {
                if (MessageBox.Show("Bạn có Chắc chắn muốn xóa???", "Note !", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    DTO_CTBangKeDichVu dto_ctbk = new DTO_CTBangKeDichVu();
                    dto_ctbk.MaCTBangKe = maCTBangKe;
                    BUS_CTBangKeDichVu.xoaCTBangKeDichVu(dto_ctbk);
                    simpleButtonHuyP_Click(sender, e);
                }
            }
            else
            {
                MessageBox.Show("Cần chọn Dịch vụ cần xóa ở Bảng Danh Sách Chi Tiết Bảng Kê!!!???", "Error !", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void simpleButtonThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void comboBoxTenLoaiDV_DropDown(object sender, EventArgs e)
        {
            dataGridViewDS.DataSource = clsKetNoi.TimKiem("pr_HienThiDichVu", "TenLoaiDV", comboBoxTenLoaiDV.Text);
        }

        private void comboBoxTenLoaiDV_DropDownClosed(object sender, EventArgs e)
        {
            dataGridViewDS.DataSource = clsKetNoi.TimKiem("pr_HienThiDichVu", "TenLoaiDV", comboBoxTenLoaiDV.Text);
        }

        
    }
}